<?php
namespace app\index\controller;
use think\Controller;
use think\Config;
use think\Cache;
use think\Session;
use think\Cookie;
use think\Db;
use app\common\model\BrowerLog;

class Index extends Controller 
{
    
    protected function _initialize()
    {
        $brower['host'] = $this->request->host(true);
        $brower['ip'] = $this->request->ip();
        $region = \Auth\Punycode::IpToCity($brower['ip']);
        if($region){
            $brower['region'] = $region['name'];
            $brower['region_id'] = $region['id'];
        }
        $brower['user_id'] = 0;
        $brower['domain_id'] = 0;
        $domain = Db::name('Domain')->where('punycode',$brower['host'])->where('status',1)->find();
        if(!empty($domain)){//找到短域名配置
            $brower['domain_id'] =$domain['id'];
            $user = Db::name('User')->where('id',$domain['userid'])->where('status',1)->find();//查找用户
            if(!empty($user)){ //找到绑定得正常用户
                $brower['user_id'] = $user['id'];
                BrowerLog::create($brower,true);
                $web = Db::name('Web')->where('domain_id',$domain['id'])->where('user_id',$domain['user_id'])->where('status',1)->find();
                if(!empty($web)){ //找到站点配置
                    if($domain['domain_type'] == '1'){ //转链
                        header('location: '.$web['url']);
                    }else{//着陆页
                        if(empty($web['area'])){
                            $rankDomain = Db::name('Config')->where('id',3)->value('value');
                            $domainArr = explode("\n",$rankDomain);
                            if(!empty($domainArr)){
                                $host = $domainArr[rand(0,count($domainArr)-1)];
                                Cache::set(md5($host.$brower['ip']),$web['id']);
                                header('location: http://'.$host.'/main.html');
                            }else{
                                Session::set('web',$web);
                                header('location: /main.html');
                            }
                        }else{//判断地区
                            if( isset($brower['region_id']) && \in_array($brower['region_id'],explode(",",$web['area']))){
                                $rankDomain = Db::name('Config')->where('id',3)->value('value');
                                $domainArr = explode("\n",$rankDomain);
                                if(!empty($domainArr)){
                                    $host = $domainArr[rand(0,count($domainArr)-1)];
                                    Cache::set(md5($host.$brower['ip']),$web['id']);
                                    header('location: http://'.$host.'/main.html');
                                }else{
                                    Session::set('web',$web);
                                    header('location: /main.html');
                                }
                            }
                        }
                        
                    }
                }
            }else{
                BrowerLog::create($brower,true);
            }
        }else{
            BrowerLog::create($brower,true);
        }
        //以上条件都不通过，进入index 默认页;请修改 index/view/index/index.html默认页;
    }
    public function index(){
        return $this->view->fetch();
    }
}